import {
    onFieldMount,
    onFieldInit,
    onFieldUnmount,
    onFieldReact,
    onFieldChange,
    onFieldValueChange,
    onFieldInitialValueChange,
    onFieldInputValueChange,
    onFieldValidateStart,
    onFieldValidateEnd,
    onFieldValidateFailed,
    onFieldValidateSuccess
} from '@formily/core'
import logic from '../logic';
import * as hooks from '@/hooks';

const ACTION_EVEN = {
    onFieldMount,
    onFieldInit,
    onFieldUnmount,
    onFieldReact,
    onFieldChange,
    onFieldValueChange,
    onFieldInitialValueChange,
    onFieldInputValueChange,
    onFieldValidateStart,
    onFieldValidateEnd,
    onFieldValidateFailed,
    onFieldValidateSuccess
}

// 设置页面元素绑定logic
export const bindLogic = (evenMap: any, custom: any) => {
    if (JSON.stringify(evenMap) == '{}' || !evenMap) {
        console.log('请先绑定事件流程');
    } else {
        // 如果是自定义事件就执行流程
        try {
            Object.keys(evenMap)?.forEach((a: any) => {
                const key = evenMap?.[a]?.['eventName'];
                // 如果是自定义事件直接执行logic
                if (key === 'customEvent') {
                    // logic(c)
                }
                // 对字段进行绑定
                if (a?.indexOf('column') > -1) {
                    ACTION_EVEN[key](evenMap?.[a]?.['elementCode'], (st: any, form: any) => {
                        getEventMap(st, key, form, evenMap?.[a]?.['imoveProcessCode'], custom)
                    })
                }
            })
        } catch (err: any) {
            console.log(`流程事件触发错误`)
        }
    }
}

// 设置map映射值
const getEventMap = (field: any, action: any, form: any, logicName: any, custom: any) => {
    // 不同的事件需要处理不同的逻辑
    if (action === 'onFieldChange') {
        if (field?.modified) {
            logic?.[logicName]?.invoke(logicName,
                {
                    form,
                    field,
                    params: { ...custom?.payload },
                    imoveHooks: {
                        ...hooks,
                        ...custom?.hooks
                    }
                })
        }
    } else {
        logic?.[logicName]?.invoke(logicName,
            {
                form,
                field,
                params: { ...custom?.payload },
                imoveHooks: {
                    ...hooks,
                    ...custom?.hooks
                }
            })
    }
}

// 给按钮绑定指定的事件
export const bindBtnEven = (o: any, eventMap: any, form: any, event: any) => {
    if (JSON.stringify(eventMap) === '{}' || !eventMap) {
        console.warn(`按钮${o?.label}请先绑定事件`)
        return
    }
    Object.keys(eventMap)?.forEach((key: any) => {
        if (eventMap?.[key]?.['elementCode'] == o.key) {
            const targer = eventMap[key]?.['imoveProcessCode']
            if (!targer) {
                console.warn(`请先绑定流程`)
                return
            }
            if (o?.props?.attributes?.isParam) {
                if (event?.[`${o.type}hook`]) {
                    console.warn(`如果按钮需要配置额外参数请配置取参数逻辑${o.type}hook`)
                }
                o.props.hook = () => {
                    logic?.[targer].invoke(targer, {
                            form: form.current, params: {
                                ...event?.[`${o?.key}${o.type}hook`]()
                            }, imoveHooks: {
                                ...hooks
                            }
                        },
                    )
                }
            } else {
                if (JSON.stringify(event) == '{}' || (!event?.[`${o.type}hook`] && event?.isParam)) {
                    console.warn(`如果按钮需要配置额外参数请配置取参数逻辑${o.type}hook`)
                }
                o.props.hook = () => {
                    logic?.[targer].invoke(targer, {
                            form: form.current, params: {
                            }, imoveHooks: {
                                ...hooks
                            }
                        },
                    )
                }
            }
        }
    })
}